<?php
/**
 * Test.php
 *
 * @package reepu.com
 * @version {$Id$}
 * @copyright 2009-2018 Shanghai Reepu Information Tech. Co., Ltd.
 * @license Commercial
 * @author Boolean since.
 * @datetime 2018-12-06 18:18
 * =================================================================
 * 版权所有 (C) 2009-2018 Reepu.com，并保留所有权利。
 * 网站地址:http://www.reepu.com/
 * -----------------------------------------------------------------
 * 您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * =================================================================
 */
namespace Reepu\Phalcon\Swoole;

use Phalcon;

class Test extends Phalcon\Cli\Task
{
    protected $namespace = 'App\\Test\\';
    protected $suffix = 'Test';

    public function mainAction(array $params = [])
    {
        if (empty($params)) {
            throw new \Exception ("Params is empty" . PHP_EOL);
        }

        if (empty($params[0])) {
            throw new \Exception ("Can't find Test File" . PHP_EOL);
        }else if (empty($params[1])) {
            throw new \Exception ("Can't find Test Action" . PHP_EOL);
        }

        $class = ucfirst(strtolower($params[0]));
        $class = "{$this->namespace}{$class}{$this->suffix}";
        $class_methods = get_class_methods($class);
        $function = $params[1];

        if (!in_array($function, $class_methods)) {
            throw new \Exception ("Action '{$function}' was not found on handler '{$class}' " . PHP_EOL);
        }

        $test = new $class();
        $test->$function();
    }

    public function __call($name, $arguments)
    {
        $class = str_replace('Action', '', $name);
        if (!isset($arguments[0][0])) {
            throw new \Exception ("Can't find Test Action" . PHP_EOL);
        }
        $function = $arguments[0][0];

        $this->mainAction([$class, $function]);
    }
}